﻿Imports System.IO
Public Class frmConvertToHTML
    Dim MyCodeBox As TextBoxPrj.CodeEditor
    Dim NeedColor As Boolean
    Dim NeedToolTip As Boolean
    Dim ColorTable As Dictionary(Of String, Color)
    Dim ToolTipTable As Dictionary(Of String, String)
    Dim SingleComment As String
    Public HTMLFileName As String
    ''' <summary>
    ''' 构造器
    ''' </summary>
    ''' <param name="CodeBox"></param>
    ''' <remarks></remarks>
    Sub New(ByVal CodeBox As TextBoxPrj.CodeEditor)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        MyCodeBox = CodeBox
        If MyCodeBox.ColorTable.Count = 0 Then
            NeedColor = False
        Else
            NeedColor = True
            ColorTable = MyCodeBox.ColorTable
        End If
        If MyCodeBox.ToolTipTable.Count = 0 Then
            NeedToolTip = False
        Else
            NeedToolTip = True
            ToolTipTable = MyCodeBox.ToolTipTable
        End If
        SingleComment = MyCodeBox.SingleComment

        '文字设定
        lblOutPut.Text = GetTextInLanguageDoc("OutPutFile", TextMode.Text)
        btnSaveFileName.Text = GetTextInLanguageDoc("Browse", TextMode.Text)
        btnConvert.Text = GetTextInLanguageDoc("Convert", TextMode.Text)
    End Sub
    ''' <summary>
    ''' 转换
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
        If txtOutPutFile.Text = String.Empty Then
            MsgBox("请输入文件名")
            Exit Sub
        Else
            HTMLFileName = txtOutPutFile.Text
        End If
        '新建一个输出流
        Dim HTMLWriter As New StreamWriter(HTMLFileName, False, System.Text.UnicodeEncoding.Unicode)
        HTMLWriter.WriteLine("<HTML>")
        HTMLWriter.WriteLine("<BODY Style='background:" & GetHTMLColor(MyCodeBox.CodeBackColor) & _
                                              ";Color:" & GetHTMLColor(MyCodeBox.CodeForeColor) & ";'>")
        HTMLWriter.WriteLine("<CODE><PRE>")      '用等宽字体输出文本
        For i As Integer = 1 To MyCodeBox.MainDocument.MaxMemoryLineNumber
            HTMLWriter.WriteLine(GetHTML(MyCodeBox.MainDocument.GetMemoryLine(i)))
        Next
        HTMLWriter.WriteLine("</PRE></CODE>")
        HTMLWriter.WriteLine("</BODY>")
        HTMLWriter.WriteLine("</HTML>")
        HTMLWriter.Close()
        Me.Close()
    End Sub
    ''' <summary>
    ''' 获得HTML文本
    ''' </summary>
    ''' <param name="LineInfo"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function GetHTML(ByVal LineInfo As TextBoxPrj.Line) As String
        With LineInfo
            '如果文本为空的话，返回空字符
            If .Text = String.Empty Then Return String.Empty
            '保存原始文字列
            Dim OrgText As String = .Text
            '如果行不包括任何单词，则退出
            If .WordList.Count = 0 Then Return OrgText
            Dim NewText As String = String.Empty

            If .Real_IsCommentStart Or .Real_IsCommentEnd Or .Real_IsCommentLine Or .Dis_IsPartyOfComment Then
                NewText &= "<Font Color='" & GetHTMLColor(MyCodeBox.CommentColor) & "'>"
                NewText &= GetHTMLString(OrgText)
                NewText &= "</Font>"
                Return NewText
            End If

            Dim NewWordList As New List(Of String)
            '如果第一个单词不是从头开始的话,第一个单词的前面部分放入列表
            If .WordList(0).StartPos > 1 Then
                NewWordList.Add(Mid(.Text, 1, .WordList(0).StartPos - 1))
            End If
            Dim TextLength As Integer = .Text.Length
            For i As Integer = 0 To .WordList.Count - 1
                NewWordList.Add(Mid(.Text, .WordList(i).StartPos, .WordList(i).Length))
                If .WordList(i).StartPos + .WordList(i).Length - 1 <> TextLength Then
                    '如果这个词没有到结尾的话
                    Dim tempStart As Integer = .WordList(i).StartPos + .WordList(i).Length
                    If i = .WordList.Count - 1 Then
                        '如果是最后一个词的话
                        NewWordList.Add(Mid(.Text, tempStart))
                    Else
                        Dim tempLength As Integer = .WordList(i + 1).StartPos - tempStart
                        NewWordList.Add(Mid(.Text, tempStart, tempLength))
                    End If
                End If
            Next

            For tempCount As Integer = 0 To NewWordList.Count - 1
                '检查这个段里是否存在注释的起始符号
                Dim IsContainSingleComment As Boolean
                Dim t As Integer
                If SingleComment = String.Empty Then
                    IsContainSingleComment = False
                Else
                    t = InStr(NewWordList(tempCount), SingleComment)
                    IsContainSingleComment = IIf(t > 0, True, False)
                End If
                If IsContainSingleComment Then
                    '如果存在的话。
                    '1 先在最终字符后，加上注释符号前的文字
                    NewText &= Mid(NewWordList(tempCount), 1, t - 1)
                    '2 加上颜色标签开始
                    NewText &= "<Font Color='" & GetHTMLColor(MyCodeBox.CommentColor) & "'>"
                    '3 加上后续文本
                    NewText &= GetHTMLString(Mid(OrgText, InStr(OrgText, SingleComment)))
                    '2 加上颜色标签结束
                    NewText &= "</Font>"
                    Exit For
                Else
                    '如果不存在的话，处理下去
                    If (NeedColor Or NeedToolTip) Then
                        NewText &= ProcessWord(NewWordList(tempCount))
                    Else
                        'HTML特殊字符的变换
                        NewText &= GetHTMLString(NewWordList(tempCount))
                    End If
                End If
            Next
            Return NewText
        End With
    End Function
    ''' <summary>
    ''' 上色和提示文本
    ''' </summary>
    ''' <param name="KeyWord"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function ProcessWord(ByVal KeyWord As String) As String
        Dim orgText As String
        orgText = KeyWord
        Dim PreWord As String = ""
        Dim TailWord As String = ""
        If NeedColor Then
            If ColorTable.ContainsKey(UCase(orgText)) Then
                PreWord = "<Font Color='" & GetHTMLColor(ColorTable.Item(UCase(orgText))) & "'>"
                TailWord = "</Font>"
            End If
        End If
        If NeedToolTip Then
            If ToolTipTable.ContainsKey(UCase(orgText)) Then
                PreWord = "<acronym Title='" & ToolTipTable(UCase(orgText)) & "'>" & PreWord
                TailWord = TailWord & "</acronym>"
            End If
        End If
        Return PreWord & GetHTMLString(orgText) & TailWord
    End Function
    ''' <summary>
    ''' 文件名
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnSaveFileName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveFileName.Click
        Using mSaveDialog As New SaveFileDialog
            mSaveDialog.Filter = "*.html(网页文件)|*.html"
            If mSaveDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                HTMLFileName = mSaveDialog.FileName
                txtOutPutFile.Text = HTMLFileName
            End If
        End Using
    End Sub
End Class
